home *** CD-ROM | disk | FTP | other *** search
- Path: news.sprintlink.net!datalytics!usenet
- From: Rob Stewart <stew@datalytics.com>
- Newsgroups: comp.lang.c++
- Subject: Re: HELP: Overloading [] operator for multidimensional array of objects.
- Date: Tue, 26 Mar 1996 12:01:21 -0500
- Organization: Datalytics, Inc
- Message-ID: <315822E1.7B07@datalytics.com>
- References: <4j6p2v$dhg@vixen.cso.uiuc.edu>
- NNTP-Posting-Host: 204.62.224.71
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Edgar L. Torres wrote:
- >
- > Hi!
- >
- > I would like to specify the overload of [], such that I may access
- > a multidimensional array of objects. As an example, given I have a
- > class
- > EXAMCLASS and I wish to dynamically allocate a three-dimensional array
- > of
- > objects:
- > EXAMCLASS object1***;
- > ...
- > object1 = malloc( sizeof(object1), dim1sz * dim2sz * dim3sz );
- > I know the above is sacrilege to C++ purists, but bare with me, since
- > the malloc
- > does work correctly in C++. Now, what I want is to be able to:
- > object1[arg1][arg2][arg3].obj1Var1 = something;
- > OR
- > something = object1[arg1][arg2][arg3].obj1Var1;
- > In general, I know I have to include three friend operator in the
- > definition of
- > class EXAMCLASS, and then define them outside:
- > EXAMCLASS** operator[](EXAMCLASS*** C, int i){
- > return ((EXAMCLASS **)(C + (i * dim2sz * dim3sz))); }
- > EXAMCLASS* operator[](EXAMCLASS** C, int i){
- > return ((EXAMCLASS *)(C + (i * dim3sz))); }
- > EXAMCLASS operator[](EXAMCLASS* C, int i){
- > return *(C + i); }
- > I know that the above does not work, but it does present the basic
- > idea of what
- > I am trying to do. If anybody has the knowledge to help with the
- > specifics of
- > syntax and argument rules, please HELP ME! Somebody suggested
- > replacing the *
- > with &, but I am not sure what EXAMCLASS&& means??
- >
- > Thanks in advance,
- > Edgar
-
- You've tried to redefine operator [], which you cannot do. It
- takes only a single dimension. The juxtaposition of multiple
- []'s invokes operator [] on different types. That is, given the
- declaration:
-
- EXAMCLASS*** p;
-
- then,
-
- p[a]
-
- refers to the a'th EXAMCLASS** in p. And,
-
- p[a][b]
-
- refers to the b'th EXAMCLASS* in the a'th EXAMCLASS** in p.
- And,
-
- p[a][b][c]
-
- refers to the c'th EXAMCLASS in the b'th EXAMCLASS* in the a'th
- EXAMCLASS** in p. Whew!
-
- The point is, you're not really looking at a multidimensional
- array. You're accessing it as one, but it isn't one. The
- solution is to use an array/vector template class parameterized
- on EXAMCLASS, then a/v<EXAMCLASS>, then a/v<a/v<EXAMCLASS>>. In
- other words, do the following (using Rogue Wave's RWTVector as
- an example vector template class):
-
- RWVector<RWVector<RWVector<EXAMCLASS>>> object1;
-
- Now you can use up to three []'s to access the various objects
- in object1.
-
- --
- Robert Stewart | My opinions are usually my own.
- Datalytics, Inc. | stew@datalytics.com
-